package entity;

import java.util.*;

public class Database 
{
	public enum DataType
	{
		String,
		Int
	}
	private String Name;
	private List<Table> tables;
	public Database()
	{
		tables = new ArrayList<Table>();
	}
	
	public Database(String Name)
	{
		this.Name = Name;
		tables = new ArrayList<Table>();
	}
	
	public String getName()
	{
		return this.Name;
	}
	
	public void setName(String Name)
	{
		this.Name = Name;
	}
	
	public boolean addTable(Table newTable)
	{
		if (getTableByName(newTable.getName())==null) 
		{
			this.tables.add(newTable);
			return true;
		}
		else return false;
	}
	
	public Table getTableAt(Integer index)
	{
		Table result=null;
		try 
		{
			result = this.tables.get(index);
			return result;
		}
		catch ( IndexOutOfBoundsException IOOBE)
		{
			return result;
		}
	}
	
	public Table getTableByName(String name)
	{
		for (int i=0;i<tables.size();i++) if (tables.get(i).getName().compareTo(name)==0) return tables.get(i);
		return null;
	}
	
	public void setTableAt(int index,Table table)
	{
		tables.set(index, table);
	}
	
	public void updateTable(Table oldTable,Table newTable)
	{
		for (int i=0;i<tables.size();i++) if (tables.get(i).getName().compareTo(oldTable.getName())==0) tables.set(i, newTable);
	}
	
	public boolean renameTable(String oldName,String newName)
	{
		Table table = getTableByName(oldName);
		if (table==null || getTableByName(newName)!=null) return false;
		table.setName(newName);
		return true;
	}
	public boolean removeTableAt(Integer index)
	{
		try 
		{
			this.tables.remove(index);
			return true;
		}
		catch ( IndexOutOfBoundsException IOOBE)
		{
			return false;
		}
	}
	
	public boolean removeTableByName(String Name)
	{
		Table table = getTableByName(Name);
		if (table==null) return false;
		return this.tables.remove(table);
	}
	
	public Integer getTablesCount()
	{
		return this.tables.size();
	}
}
